import { mapState, mapActions } from 'vuex'

export default {
  data () {
    return {
      provinceListData: [], // 省份列表
      regionListProps: {
        value: 'id',
        label: 'name',
        children: 'childrenList',
        lazy: true,
        lazyLoad: async (node, resolve) => {
          let { level, pathNodes } = node
          let nodes = []
          switch (level) {
            case 1:
              await this.getCityListData(pathNodes[0].value)
              nodes = this.cityList.map((item) => {
                return {
                  'id': `${item.city}`,
                  'name': item.name,
                  'childrenList': []
                }
              })
              break
            case 2:
              await this.getCountyListData(pathNodes[0].value, pathNodes[1].value)
              nodes = this.countyList.map((item) => {
                return {
                  'id': `${item.area}`,
                  'name': item.name,
                  'leaf': true
                }
              })
              break
          }
          // 通过调用resolve将子节点数据返回，通知组件数据加载完成
          resolve(nodes)
        }
      }
    }
  },
  computed: {
    ...mapState('region', {
      provinceList: state => state.provinceList,
      cityList: state => state.cityList,
      countyList: state => state.countyList
    })
  },
  methods: {
    ...mapActions('region', {
      getProvinceList: 'GetProvinceListData', // 获取省
      getCityList: 'GetCityListData', // 获取市
      getCountyList: 'GetCountyListData' // 获取区县
    }),
    handleRegionChange (val) { // 省市区联动修改
      console.log(val)
    },
    async getProvinceListData () {
      try {
        // let response =
        await this.getProvinceList({})
        // let result = response.data
        this.provinceListData = this.provinceList.map((item) => {
          return {
            'id': `${item.province}`,
            'name': item.name,
            'childrenList': []
          }
        })
      } catch (error) {
        // console.log(error)
        this.$notify.error({
          title: '错误',
          message: error.message
        })
      }
    },
    async getCityListData (provId) {
      try {
        await this.getCityList({
          pid: provId
        })
      } catch (error) {
        // console.log(error)
        this.$notify.error({
          title: '错误',
          message: error.message
        })
      }
    },
    async getCountyListData (provId, cityId) {
      try {
        await this.getCountyList({
          pid: provId,
          cid: cityId
        })
      } catch (error) {
        // console.log(error)
        this.$notify.error({
          title: '错误',
          message: error.message
        })
      }
    }
  },
  watch: {}
}
